﻿using System;

namespace MvvmFx.Common.Helpers
{
    /// <summary>
    /// Static utility class containing helper methods for the Dispatcher.
    /// </summary>
    public static partial class DispatcherHelper
    {
     /// <summary>
        /// Invoke the specified action asynchronously with a Background priority
        /// </summary>
        /// <param name="action"></param>
        public static void BeginInvoke(Action action)
        {
            var dispatcher = GetCurrent();
            if (dispatcher == null)
            {
                action();
            }
            else
            {
                dispatcher.BeginInvoke(action);
            }
        }

        /// <summary>
        /// Determines whether the calling thread is the thread associated with this System.Windows.Threading.Dispatcher.
        /// </summary>
        /// <returns>true if the calling thread is the thread associated with this System.Windows.Threading.Dispatcher; otherwise, false.</returns>
        public static bool CheckAccess()
        {
            var dispatcher = GetCurrent();
            if (dispatcher == null)
            {
                // This will be null in a unit test
                return true;
            }

            return dispatcher.CheckAccess();
        }
    }
}